$(function () {
    Time();
    load();
})

function Time() {
    var time = new Date();
    var y = time.getFullYear();
    var mon = time.getMonth() + 1; //0-11
    var d = time.getDate();
    var h = time.getHours();
    var m = time.getMinutes();
    var s = time.getSeconds();
    $("#datetime").val(y + "-" + mon + "-" + d + " " + h + ":" + m + ":" + s);
}

let list = [];

function load() {
    let user = JSON.parse(localStorage.getItem("user"));
    $("#rname").val(user.rname);
    let MMADD_mManufacture = JSON.parse(localStorage.getItem("MMADD_mManufacture"));
    $("#designer").val(MMADD_mManufacture.designer);
    $("#remark").val(MMADD_mManufacture.remark);
    let MMADD = JSON.parse(localStorage.getItem("MMADD(2)"));
    $("#ProductId").text(MMADD.productid);
    $("#ProductName").text(MMADD.productname);
    $("#amount").text(MMADD.amount);
    $("#productdescribe").text(MMADD.productdescribe);

    console.log(MMADD)
    $.post("/Mmanufacture.do/mManufactureCha", {id: MMADD.productid}, function (sdata) {
        $("#mapID").text(sdata.designid);
        let str = " <tr>\n" +
            "          <th>序号</th>\n" +
            "          <th>工序名称</th>\n" +
            "          <th>工序编号</th>\n" +
            "          <th>描述</th>\n" +
            "          <th>工时数（h）</th>\n" +
            "          <th>单位工时成本（元）</th>\n" +
            "          <th>工时成本小计(元)</th>\n" +
            "          <th>查看物料</th>\n" +
            "      </tr>\n";
        list = sdata.mdesignproceduredetailsList;
        $.each(sdata.mdesignproceduredetailsList, function (index, item) {
            let s = JSON.stringify(item.mdesignproceduremoduledetailsList);
            str += "      <tr>\n" +
                "          <td>" + item.id + "</td>\n" +
                "          <td>" + item.procedurename + "</td>\n" +
                "          <td>" + item.procedureid + "</td>\n" +
                "          <td>" + item.proceduredescribe + "</td>\n" +
                "          <td><span class='costprice'>" + (item.costprice*MMADD.amount) + "</span> </td>\n" +
                "          <td><span class='subtotal'>" + item.subtotal + "</span></td>\n" +
                "          <td><span class='modulesubtotal'>" + ((item.costprice*MMADD.amount)*item.subtotal) + "</span> </td>\n" +
                "          <td><a href=\"#\" onclick='XQ03(" + s + ",\"" + item.procedurename + "\")'>查看物料</a> </td>\n" +
                "      </tr>";
        })
        $("#tableAdd02").html(str);
    })
}

function fh() {
    localStorage.getItem("MMADD(1)");
    localStorage.getItem("MMADD_mManufacture");
    localStorage.getItem("MMADD(2)");
    location.href = "../../wen_zhi_yi/mManufactureAdd.html";
}

function XQ03(op, dname) {
    $("#XQ03Modal").modal("show");
    $("#dname").text(dname);
    let sstr = " <tr>\n" +
        "          <th>序号</th>\n" +
        "          <th>物料名称</th>\n" +
        "          <th>物料编号</th>\n" +
        "          <th>描述</th>\n" +
        "          <th>本工序数量</th>\n" +
        "          <th>单位</th>\n" +
        "          <th>单价（元）</th>\n" +
        "          <th>小计(元)</th>\n" +
        "      </tr>\n";
    $.each(op, function (index, item) {
        sstr += "      <tr>\n" +
            "          <td>" + item.id + "</td>\n" +
            "          <td>" + item.productname + "</td>\n" +
            "          <td>" + item.productid + "</td>\n" +
            "          <td>" + item.productdescribe + "</td>\n" +
            "          <td>" + item.amount + "</td>\n" +
            "          <td>" + item.amountunit + "</td>\n" +
            "          <td>" + item.costprice + "</td>\n" +
            "          <td>" + item.subtotal + "</td>\n" +
            "      </tr>";
    })
    $("#tm01").html(sstr);
}

function submit() {
    let XJ = 0;
    let xiaoji =[];
    $(".modulesubtotal").each(function () {
        xiaoji.push(parseInt($(this).text()));
        XJ += parseInt($(this).text());
    });
    let sls = 0;
    for (let j = 0; j < list.length; j++) {
        $.each(list[j].mdesignproceduremoduledetailsList, function (index, item) {
            sls += item.amount * item.costprice;
        })
    }
    let MMADD = JSON.parse(localStorage.getItem("MMADD(2)"));
    // 生产总表Add
    let mManufacture = {
        "productid": MMADD.productid,
        "productname": MMADD.productname,
        "productdescribe": MMADD.productdescribe,
        "amount": $("#amount").text(),
        "modulecostpricesum": sls,
        "labourcostpricesum": XJ,
        "designer": $("#designer").val(),
        "register": $("#rname").val(),
        "registertime": $("#datetime").val(),
        "remark": $("#remark").val(),
        "checktag": "S001-0",
        "manufactureproceduretag": "S002-0",
    };
    console.log(mManufacture)
    let shu = [];
    $(".costprice").each(function () {
        shu.push($(this).text());
    })
    let subtotal = [];
    $(".subtotal").each(function () {
        subtotal.push($(this).text());
    })
    let mProcedures = [];
    for (let i = 0; i < list.length; i++) {
        let MpLists =[];
        for (let j = 0; j < (list[i].mdesignproceduremoduledetailsList).length; j++) {
            let mProcedureModule = {
                "productid": (list[i].mdesignproceduremoduledetailsList)[j].productid,
                "productname": (list[i].mdesignproceduremoduledetailsList)[j].productname,
                "costprice": (list[i].mdesignproceduremoduledetailsList)[j].costprice,
                "amount": (list[i].mdesignproceduremoduledetailsList)[j].amount,
                "renewamount": (list[i].mdesignproceduremoduledetailsList)[j].amount,
                "subtotal": (list[i].mdesignproceduremoduledetailsList)[j].subtotal
            };
            MpLists[j] = mProcedureModule;
        }
        let mProcedure = {
            "detailsnumber": list[i].detailsnumber,
            "procedureid": list[i].procedureid,
            "procedurename": list[i].procedurename,
            "labourhouramount": shu[i],
            "subtotal": xiaoji[i],
            "modulesubtotal":  list[i].modulesubtotal,
            "costprice": subtotal[i],
            "procedurefinishtag": "G004-0",
            "MpLists":MpLists
        };
        mProcedures[i] = mProcedure;
    }
    let IDS = JSON.parse(localStorage.getItem("MMADD(1)"));
    let mApplyID = [];
    for (let g = 0; g < IDS.length; g++) {
        mApplyID[g] = IDS[g].id;
    }
    console.log(mApplyID)
    let ids = JSON.stringify(mApplyID);
    let mpf = JSON.stringify(mManufacture);
    let mpd = JSON.stringify(mProcedures);

    $.post("/Mmanufacture.do/mManufactureAdd", {
        mManufacture: mpf,
        mProcedures: mpd,
        ids: ids
    }, function (sd) {
        $("#tishi").text("派工成功！")
        $("#tishi").css("color", "lime")
        $("#staticBackdrop").modal("show");
    })
}

function hh() {
    localStorage.getItem("MMADD(1)");
    localStorage.getItem("MMADD_mManufacture");
    localStorage.getItem("MMADD(2)");
    location.href = "../../wen_zhi_yi/mManufactureAdd.html";
}







